package zhr.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import zhr.store.common.CustomException;
import zhr.store.domain.Category;

import zhr.store.domain.Dish;
import zhr.store.domain.Setmeal;
import zhr.store.service.CategoryService;
import zhr.store.mapper.CategoryMapper;
import org.springframework.stereotype.Service;
import zhr.store.service.DishService;
import zhr.store.service.SetmealService;


/**
* @author 詹华榕
* @description 针对表【category(零食及套餐分类)】的数据库操作Service实现
* @createDate 2023-04-15 15:14:43
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{

    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    /**
     * 删除分类
     *
     * @param id
     */
    @Override
    public void remove(Long id) {
        //判断分类id是否关联菜品
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //查询条件
        dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
        int countDish = dishService.count(dishLambdaQueryWrapper);
        if (countDish > 0) {
            //关联菜品,抛出异常
            throw new CustomException("当前分类关联菜品,无法删除");
        }
        //判断分类id是否关联套餐
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //查询条件
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
        int countSetmeal = setmealService.count(setmealLambdaQueryWrapper);
        if (countSetmeal > 0) {
            //关联套餐,抛出异常
            throw new CustomException("当前分类关联套餐,无法删除");
        }
        //删除分类
        super.removeById(id);
    }
}




